/**
 * 版权所有 @鸿名物联 
 * 未经授权，禁止侵权和商业，违法必究
 * 联系QQ：2224313811 
 * 
 */
package com.lp.controller.iot;

import com.lp.bean.TbBaseHazard;
import com.lp.bo.IotSceneInfoBO;
import com.lp.bo.UserInfoBO;
import com.lp.cache.CacheName;
import com.lp.cache.ProCacheUtil;
import com.lp.common.Code;
import com.lp.common.Constants;
import com.lp.common.RequestURLIOT;
import com.lp.controller.BaseController;
import com.lp.util.ObjectUtil;
import com.lp.util.ResultMapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;


@Controller
public class TbBaseHazardController extends BaseController {

	/**
	 * 检索
	 * @param response
	 * @param 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.BaseHazard.BASE_HAZARD_PAGE)
	public ModelAndView selectPage(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody TbBaseHazard obj,
			@RequestParam(required=false) Integer pageSize ,
			@RequestParam Integer paged ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			resultMap = service.selectPageList("TbBaseHazard.select",getPageBean(paged,pageSize), obj);
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}
	
	/**
	 * 超管使用检索
	 * @param response
	 * @param 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.BaseHazard.ADMIN_BASE_HAZARD_PAGE)
	public ModelAndView selectALLPage(HttpServletResponse response, 
			@RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
			@RequestBody IotSceneInfoBO obj,
			@RequestParam(required=false) Integer pageSize ,
			@RequestParam Integer paged ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			if(user.getType() == Code.UserType.SUPER ){
				resultMap = service.selectPageList("TbBaseHazard.select",getPageBean(paged,pageSize), obj);
			}
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}

	/**
	 * 插入
	 * @param response
	 * @param
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = RequestURLIOT.BaseHazard.BASE_HAZARD)
	public ModelAndView save(HttpServletResponse response,
							 @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
							 @RequestBody TbBaseHazard obj ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			if(ObjectUtil.isNotEmpty(user)){
				IotSceneInfoBO iotSceneInfoBO = new IotSceneInfoBO();
				iotSceneInfoBO.setCq_company_code(obj.getCompany_code());
				IotSceneInfoBO infoBO = (IotSceneInfoBO)getData(service.selectOne("IotSceneInfo.select", iotSceneInfoBO));
				if(ObjectUtil.isNotEmpty(infoBO)) {
					TbBaseHazard tbBaseHazard = (TbBaseHazard)getData(service.selectOne("TbBaseHazard.selectMaxCode", obj.getCompany_code()));
					if(ObjectUtil.isNotEmpty(tbBaseHazard)) {
						String str = tbBaseHazard.getHazard_code().substring(tbBaseHazard.getHazard_code().length() - 3);
						int num = Integer.parseInt(str) + 1;
						obj.setHazard_code(obj.getCompany_code() + String.format("%03d", num));
					} else {
						obj.setHazard_code(obj.getCompany_code() + "001");
					}
					obj.setCreate_by(user.getName());
					obj.setCreate_date(new Date());
					obj.setUpdate_by(user.getName());
					obj.setUpdate_date(new Date());
					obj.setStatus(Constants.DELETE.NO);
					resultMap = service.insert("TbBaseHazard.insert", obj) ;
					if(isOk(resultMap)){
						// 添加缓存
						ProCacheUtil.addCache(CacheName.BASE_HAZARD, obj.getId().toString(), obj);
					}
				} else {
					putStatusCode(resultMap, Code.ResponseCode.SystemCode.ERROR , "企业不存在") ;
				}
			}else{
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.NO_AUTHORIZATION);
			}
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}

	/**
	 * 查询单个
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = RequestURLIOT.BaseHazard.BASE_HAZARD)
	public ModelAndView selectOne(HttpServletResponse response,
								  @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
								  @RequestParam Integer id ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			TbBaseHazard obj = new TbBaseHazard();
			UserInfoBO user = getUserInfoByUserKey(userKey);
			if(ObjectUtil.isNotEmpty(user)  ){
				obj.setId(id);
				resultMap = service.selectOne("TbBaseHazard.selectOne", obj) ;
			}else{
				putStatusCode(resultMap, Code.ResponseCode.SystemCode.ERROR);
			}
		} catch (Exception e) {
			exception(e,resultMap, id);
		}
		return getModelAndView(response, resultMap);
	}

	/**
	 * 更新
	 * @param response
	 * @param obj
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT, value = RequestURLIOT.BaseHazard.BASE_HAZARD)
	public ModelAndView update(HttpServletResponse response,
							   @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
							   @RequestBody TbBaseHazard obj ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			UserInfoBO user = getUserInfoByUserKey(userKey);
			obj.setUpdate_by(user.getName());
			resultMap = service.update("TbBaseHazard.update", obj) ;
			if(isOk(resultMap)){
				// 添加缓存
				ProCacheUtil.addCache(CacheName.BASE_HAZARD, obj.getId().toString(), obj);
			}
		} catch (Exception e) {
			exception(e,resultMap, obj);
		}
		return getModelAndView(response, resultMap);
	}

	/**
	 * 删除
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.DELETE, value = RequestURLIOT.BaseHazard.BASE_HAZARD)
	public ModelAndView delete(HttpServletResponse response,
							   @RequestHeader(value = ResultMapUtils.USER_KEY, required = true) String userKey ,
							   @RequestParam Integer id ) {
		Map<String, Object> resultMap = getResultMap();
		try {
			TbBaseHazard obj = new TbBaseHazard();
			obj.setId(id);
			obj.setStatus(Constants.DELETE.YES);
			resultMap = service.update("TbBaseHazard.update", obj);
			if(isOk(resultMap)){
				// 添加缓存
				ProCacheUtil.removeCache(CacheName.BASE_HAZARD, obj.getId().toString());
			}
		} catch (Exception e) {
			exception(e,resultMap, id);
		}
		return getModelAndView(response, resultMap);
	}

}
